<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>服务端配置 | 一枚程序猿的blog</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/blog-assets/img/logo.svg">
    <meta name="description" content="墨陈的个人博客">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog-assets/assets/css/0.styles.b8ba274c.css" as="style"><link rel="preload" href="/blog-assets/assets/js/app.8304d51e.js" as="script"><link rel="preload" href="/blog-assets/assets/js/4.08820d5d.js" as="script"><link rel="preload" href="/blog-assets/assets/js/1.f05623d2.js" as="script"><link rel="preload" href="/blog-assets/assets/js/13.d806e265.js" as="script"><link rel="prefetch" href="/blog-assets/assets/js/10.0d69c58c.js"><link rel="prefetch" href="/blog-assets/assets/js/11.16049b38.js"><link rel="prefetch" href="/blog-assets/assets/js/12.118c5714.js"><link rel="prefetch" href="/blog-assets/assets/js/14.288551ad.js"><link rel="prefetch" href="/blog-assets/assets/js/15.4e026621.js"><link rel="prefetch" href="/blog-assets/assets/js/16.64396396.js"><link rel="prefetch" href="/blog-assets/assets/js/17.a4bcdb0e.js"><link rel="prefetch" href="/blog-assets/assets/js/18.9b560985.js"><link rel="prefetch" href="/blog-assets/assets/js/19.93cf4bca.js"><link rel="prefetch" href="/blog-assets/assets/js/20.1ca10da7.js"><link rel="prefetch" href="/blog-assets/assets/js/21.531cde56.js"><link rel="prefetch" href="/blog-assets/assets/js/22.0effffc5.js"><link rel="prefetch" href="/blog-assets/assets/js/23.b12e3c44.js"><link rel="prefetch" href="/blog-assets/assets/js/24.b90b9dec.js"><link rel="prefetch" href="/blog-assets/assets/js/3.2ea7ac7b.js"><link rel="prefetch" href="/blog-assets/assets/js/5.3e2f5e91.js"><link rel="prefetch" href="/blog-assets/assets/js/6.a5fb46dd.js"><link rel="prefetch" href="/blog-assets/assets/js/7.f09ff05b.js"><link rel="prefetch" href="/blog-assets/assets/js/8.0c2b6c52.js"><link rel="prefetch" href="/blog-assets/assets/js/9.8858b38f.js">
    <link rel="stylesheet" href="/blog-assets/assets/css/0.styles.b8ba274c.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div id="theme-container" class="theme-container" data-v-57e42542><header class="navbar is-fixed is-visible invert" data-v-57e42542><a href="/blog-assets/" class="home-link router-link-active"><span class="site-name">一枚程序猿的blog</span></a> <div class="links"><nav class="nav-links can-hide"><div class="nav-item"><a href="/blog-assets/" class="nav-link ov-parent ov-hover"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path d="M489.2 287.9h-27.4c-2.6 0-4.6 2-4.6 4.6v32h-36.6V146.2c0-2.6-2-4.6-4.6-4.6h-27.4c-2.6 0-4.6 2-4.6 4.6v32h-36.6v-32c0-2.6-2-4.6-4.6-4.6h-27.4c-2.6 0-4.6 2-4.6 4.6v32h-36.6v-32c0-6-8-4.6-11.7-4.6v-38c8.3-2 17.1-3.4 25.7-3.4 10.9 0 20.9 4.3 31.4 4.3 4.6 0 27.7-1.1 27.7-8v-60c0-2.6-2-4.6-4.6-4.6-5.1 0-15.1 4.3-24 4.3-9.7 0-20.9-4.3-32.6-4.3-8 0-16 1.1-23.7 2.9v-4.9c5.4-2.6 9.1-8.3 9.1-14.3 0-20.7-31.4-20.8-31.4 0 0 6 3.7 11.7 9.1 14.3v111.7c-3.7 0-11.7-1.4-11.7 4.6v32h-36.6v-32c0-2.6-2-4.6-4.6-4.6h-27.4c-2.6 0-4.6 2-4.6 4.6v32H128v-32c0-2.6-2-4.6-4.6-4.6H96c-2.6 0-4.6 2-4.6 4.6v178.3H54.8v-32c0-2.6-2-4.6-4.6-4.6H22.8c-2.6 0-4.6 2-4.6 4.6V512h182.9v-96c0-72.6 109.7-72.6 109.7 0v96h182.9V292.5c.1-2.6-1.9-4.6-4.5-4.6zm-288.1-4.5c0 2.6-2 4.6-4.6 4.6h-27.4c-2.6 0-4.6-2-4.6-4.6v-64c0-2.6 2-4.6 4.6-4.6h27.4c2.6 0 4.6 2 4.6 4.6v64zm146.4 0c0 2.6-2 4.6-4.6 4.6h-27.4c-2.6 0-4.6-2-4.6-4.6v-64c0-2.6 2-4.6 4.6-4.6h27.4c2.6 0 4.6 2 4.6 4.6v64z"/></svg>
  首页
</a></div><div class="nav-item"><a href="/blog-assets/tags/" class="nav-link ov-parent ov-hover"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path d="M0 252.118V48C0 21.49 21.49 0 48 0h204.118a48 48 0 0133.941 14.059l211.882 211.882c18.745 18.745 18.745 49.137 0 67.882L293.823 497.941c-18.745 18.745-49.137 18.745-67.882 0L14.059 286.059A48 48 0 010 252.118zM112 64c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48-21.49-48-48-48z"/></svg>
  标签
</a></div><div class="nav-item"><a href="/blog-assets/links/" class="nav-link ov-parent ov-hover"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path d="M305.45 462.59c7.391 7.298 6.188 20.097-3 25.004-77.714 41.802-176.726 29.91-242.344-35.709-65.602-65.603-77.51-164.523-35.692-242.331 4.891-9.095 17.69-10.298 25.003-3l116.812 116.813 27.394-27.394c-.688-2.61-1.594-5.001-1.594-7.814a32.004 32.004 0 1132.004 32.005c-2.797 0-5.204-.891-7.798-1.594l-27.41 27.41zm206.526-159.523a16.103 16.103 0 01-16.002 17.003H463.86a15.97 15.97 0 01-15.892-15.002C440.467 175.549 336.453 70.534 207.03 63.533a15.845 15.845 0 01-15.002-15.908V16.027A16.094 16.094 0 01209.03.024C372.255 8.62 503.475 139.841 511.976 303.067zm-96.012-.297a16.21 16.21 0 01-16.112 17.3h-32.207a16.069 16.069 0 01-15.893-14.705c-6.907-77.011-68.118-138.91-144.924-145.224a15.94 15.94 0 01-14.8-15.893v-32.114a16.134 16.134 0 0117.3-16.096c110.123 8.501 198.228 96.607 206.636 206.732z"/></svg>
  链接
</a></div><div class="nav-item"><a href="/blog-assets/docs/" aria-current="page" class="nav-link ov-parent ov-hover router-link-exact-active router-link-active"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="0 0 24 24" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path fill="none" d="M0 0h24v24H0z"/><path d="M21 18H6a1 1 0 000 2h15v2H6a3 3 0 01-3-3V4a2 2 0 012-2h16v16zm-5-9V7H8v2h8z"/></svg>
  文档
</a></div><div class="nav-item"><a href="https://github.com/mochen1942" target="_blank" rel="noopener noreferrer" class="nav-link external ov-parent ov-hover"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="0 0 512 512" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path d="M256 32C132.3 32 32 134.9 32 261.7c0 101.5 64.2 187.5 153.2 217.9a17.56 17.56 0 003.8.4c8.3 0 11.5-6.1 11.5-11.4 0-5.5-.2-19.9-.3-39.1a102.4 102.4 0 01-22.6 2.7c-43.1 0-52.9-33.5-52.9-33.5-10.2-26.5-24.9-33.6-24.9-33.6-19.5-13.7-.1-14.1 1.4-14.1h.1c22.5 2 34.3 23.8 34.3 23.8 11.2 19.6 26.2 25.1 39.6 25.1a63 63 0 0025.6-6c2-14.8 7.8-24.9 14.2-30.7-49.7-5.8-102-25.5-102-113.5 0-25.1 8.7-45.6 23-61.6-2.3-5.8-10-29.2 2.2-60.8a18.64 18.64 0 015-.5c8.1 0 26.4 3.1 56.6 24.1a208.21 208.21 0 01112.2 0c30.2-21 48.5-24.1 56.6-24.1a18.64 18.64 0 015 .5c12.2 31.6 4.5 55 2.2 60.8 14.3 16.1 23 36.6 23 61.6 0 88.2-52.4 107.6-102.3 113.3 8 7.1 15.2 21.1 15.2 42.5 0 30.7-.3 55.5-.3 63 0 5.4 3.1 11.5 11.4 11.5a19.35 19.35 0 004-.4C415.9 449.2 480 363.1 480 261.7 480 134.9 379.7 32 256 32z"/></svg>
  仓库地址
</a></div> <div class="nav-item"><a class="nav-link ov-parent ov-hover" style="cursor: pointer"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="0 0 24 24" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg>
      搜索
    </a></div></nav></div></header> <div class="sidebar-mask" data-v-57e42542></div> <aside class="sidebar" data-v-57e42542><div class="personal-info-wrapper" data-v-57e42542><div class="mobile-hero-avatar" data-v-57e42542><img src="/blog-assets/img/avatar.jpeg" alt="hero" data-v-57e42542></div> <p class="mobile-heading" data-v-57e42542>
          墨陈
        </p> <div class="sns-wrapper" data-v-57e42542><a href="https://github.com/mochen1942" target="_blank" rel="noopener noreferrer"><svg aria-hidden="true" width="0" height="0" viewBox="0 0 0 0" fill="currentColor" class="icon-stack ov-icon" style="font-size:1.2em;"><!----> <svg aria-hidden="true" width="19.2" height="19.2" viewBox="-59.52 -43.52 599.04 599.04" fill="currentColor" class="icon-sns ov-icon ov-inverse" style="font-size:1.2em;"><path d="M186.1 328.7c0 20.9-10.9 55.1-36.7 55.1s-36.7-34.2-36.7-55.1 10.9-55.1 36.7-55.1 36.7 34.2 36.7 55.1zM480 278.2c0 31.9-3.2 65.7-17.5 95-37.9 76.6-142.1 74.8-216.7 74.8-75.8 0-186.2 2.7-225.6-74.8-14.6-29-20.2-63.1-20.2-95 0-41.9 13.9-81.5 41.5-113.6-5.2-15.8-7.7-32.4-7.7-48.8 0-21.5 4.9-32.3 14.6-51.8 45.3 0 74.3 9 108.8 36 29-6.9 58.8-10 88.7-10 27 0 54.2 2.9 80.4 9.2 34-26.7 63-35.2 107.8-35.2 9.8 19.5 14.6 30.3 14.6 51.8 0 16.4-2.6 32.7-7.7 48.2 27.5 32.4 39 72.3 39 114.2zm-64.3 50.5c0-43.9-26.7-82.6-73.5-82.6-18.9 0-37 3.4-56 6-14.9 2.3-29.8 3.2-45.1 3.2-15.2 0-30.1-.9-45.1-3.2-18.7-2.6-37-6-56-6-46.8 0-73.5 38.7-73.5 82.6 0 87.8 80.4 101.3 150.4 101.3h48.2c70.3 0 150.6-13.4 150.6-101.3zm-82.6-55.1c-25.8 0-36.7 34.2-36.7 55.1s10.9 55.1 36.7 55.1 36.7-34.2 36.7-55.1-10.9-55.1-36.7-55.1z"/></svg></svg></a> <a href="https://github.com/mochen1942" target="_blank" rel="noopener noreferrer"><svg aria-hidden="true" width="0" height="0" viewBox="0 0 0 0" fill="currentColor" class="icon-stack ov-icon" style="font-size:1.2em;"><!----> <svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="icon-sns ov-icon ov-inverse" style="font-size:1.2em;"><path d="M256 8C119.033 8 8 119.033 8 256s111.033 248 248 248 248-111.033 248-248S392.967 8 256 8zm117.134 346.753c-1.592 1.867-39.776 45.731-109.851 45.731-84.692 0-144.484-63.26-144.484-145.567 0-81.303 62.004-143.401 143.762-143.401 66.957 0 101.965 37.315 103.422 38.904a12 12 0 011.238 14.623l-22.38 34.655c-4.049 6.267-12.774 7.351-18.234 2.295-.233-.214-26.529-23.88-61.88-23.88-46.116 0-73.916 33.575-73.916 76.082 0 39.602 25.514 79.692 74.277 79.692 38.697 0 65.28-28.338 65.544-28.625 5.132-5.565 14.059-5.033 18.508 1.053l24.547 33.572a12.001 12.001 0 01-.553 14.866z"/></svg></svg></a><a href="https://github.com/mochen1942" target="_blank" rel="noopener noreferrer"><svg aria-hidden="true" width="0" height="0" viewBox="0 0 0 0" fill="currentColor" class="icon-stack ov-icon" style="font-size:1.2em;"><!----> <svg aria-hidden="true" width="19.2" height="19.2" viewBox="-3.2 -3.2 38.4 38.4" fill="currentColor" class="icon-sns ov-icon ov-inverse" style="font-size:1.2em;"><path d="M31.385 16.995c-.807-1.839-2.344-3.297-4.12-4.198-3.141-1.589-7.042-1.583-10.172.042-2.177 1.115-3.99 3.115-4.505 5.547-.427 1.792-.047 3.719.906 5.281 1.417 2.349 3.974 3.849 6.63 4.328 1.922.391 3.911.161 5.781-.375 1.125.432 2.125 1.135 3.214 1.661a66.909 66.909 0 00-.906-2.807c1.224-.865 2.328-1.948 2.995-3.302.99-1.885 1.052-4.229.177-6.177zM18.156 4.375c-3.328-1.87-7.469-2.151-11.047-.859-2.354.849-4.479 2.422-5.797 4.563C.119 10.011-.329 12.423.249 14.631c.573 2.453 2.281 4.51 4.349 5.885a106.295 106.295 0 00-1.141 3.406c1.297-.677 2.599-1.391 3.901-2.078 1.557.5 3.219.745 4.88.688-.443-1.255-.542-2.62-.339-3.932.297-1.828 1.297-3.5 2.656-4.74 2.302-2.156 5.563-3.063 8.661-2.844-.583-2.844-2.583-5.25-5.099-6.641zm2.313 13.318c-.281.87-1.542 1.13-2.156.469-.672-.609-.411-1.891.474-2.161.979-.411 2.109.714 1.682 1.693zm6.39.125c-.339.781-1.521.964-2.099.359-.281-.255-.359-.641-.458-.974.135-.609.557-1.245 1.24-1.271.938-.13 1.781 1.031 1.297 1.885zm-9.541-8.604c.01 1.281-1.698 2.078-2.656 1.208-1-.714-.714-2.448.453-2.807 1.047-.417 2.286.458 2.203 1.583zm-8.078.322c-.229 1.115-1.719 1.651-2.594.906-1.01-.714-.729-2.484.458-2.844 1.167-.448 2.49.734 2.135 1.938z"/></svg></svg></a><a href="https://github.com/mochen1942" target="_blank" rel="noopener noreferrer"><svg aria-hidden="true" width="0" height="0" viewBox="0 0 0 0" fill="currentColor" class="icon-stack ov-icon" style="font-size:1.2em;"><!----> <svg aria-hidden="true" width="19.2" height="19.2" viewBox="-3.2 -3.2 38.4 38.4" fill="currentColor" class="icon-sns ov-icon ov-inverse" style="font-size:1.2em;"><path d="M29.11 26.278c-.72.087-2.804-3.296-2.804-3.296 0 1.959-1.009 4.515-3.191 6.362 1.052.325 3.428 1.198 2.863 2.151-.457.772-7.844.493-9.977.252-2.133.24-9.52.519-9.977-.252-.565-.953 1.807-1.826 2.861-2.151-2.182-1.846-3.191-4.403-3.191-6.362 0 0-2.083 3.384-2.804 3.296-.335-.041-.776-1.853.584-6.231.641-2.064 1.375-3.78 2.509-6.611C5.792 6.13 8.811.001 15.999.001c7.109.001 10.197 6.008 10.017 13.435 1.132 2.826 1.869 4.553 2.509 6.611 1.361 4.379.92 6.191.584 6.231z"/></svg></svg></a><a href="https://github.com/mochen1942" target="_blank" rel="noopener noreferrer"><svg aria-hidden="true" width="0" height="0" viewBox="0 0 0 0" fill="currentColor" class="icon-stack ov-icon" style="font-size:1.2em;"><!----> <svg aria-hidden="true" width="19.2" height="19.2" viewBox="0 0 24 24" fill="currentColor" class="icon-sns ov-icon ov-inverse" style="font-size:1.2em;"><path fill="none" d="M0 0h24v24H0z"/><path d="M12.344 17.963l-1.688 1.074-2.131-3.35c-.44 1.402-1.172 2.665-2.139 3.825-.402.483-.82.918-1.301 1.375-.155.147-.775.717-.878.82l-1.414-1.414c.139-.139.787-.735.915-.856.43-.408.795-.79 1.142-1.206 1.266-1.518 2.03-3.21 2.137-5.231H3v-2h4V7h-.868c-.689 1.266-1.558 2.222-2.618 2.857L2.486 8.143c1.395-.838 2.425-2.604 3.038-5.36l1.952.434c-.14.633-.303 1.227-.489 1.783H11.5v2H9v4h2.5v2H9.185l3.159 4.963zm3.838-.07L17.298 17H19V7h-4v10h.736l.446.893zM13 5h8v14h-3l-2.5 2-1-2H13V5z"/></svg></svg></a><a href="mailto:2327772505@qq.com" target="_blank" rel="noopener noreferrer"><svg aria-hidden="true" width="0" height="0" viewBox="0 0 0 0" fill="currentColor" class="icon-stack ov-icon" style="font-size:1.2em;"><!----> <svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="icon-sns ov-icon ov-inverse" style="font-size:1.2em;"><path d="M502.3 190.8c3.9-3.1 9.7-.2 9.7 4.7V400c0 26.5-21.5 48-48 48H48c-26.5 0-48-21.5-48-48V195.6c0-5 5.7-7.8 9.7-4.7 22.4 17.4 52.1 39.5 154.1 113.6 21.1 15.4 56.7 47.8 92.2 47.6 35.7.3 72-32.8 92.3-47.6 102-74.1 131.6-96.3 154-113.7zM256 320c23.2.4 56.6-29.2 73.4-41.4 132.7-96.3 142.8-104.7 173.4-128.7 5.8-4.5 9.2-11.5 9.2-18.9v-19c0-26.5-21.5-48-48-48H48C21.5 64 0 85.5 0 112v19c0 7.4 3.4 14.3 9.2 18.9 30.6 23.9 40.7 32.4 173.4 128.7 16.8 12.2 50.2 41.8 73.4 41.4z"/></svg></svg></a> <!----></div> <hr data-v-57e42542></div> <nav class="nav-links"><div class="nav-item"><a href="/blog-assets/" class="nav-link ov-parent ov-hover"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path d="M489.2 287.9h-27.4c-2.6 0-4.6 2-4.6 4.6v32h-36.6V146.2c0-2.6-2-4.6-4.6-4.6h-27.4c-2.6 0-4.6 2-4.6 4.6v32h-36.6v-32c0-2.6-2-4.6-4.6-4.6h-27.4c-2.6 0-4.6 2-4.6 4.6v32h-36.6v-32c0-6-8-4.6-11.7-4.6v-38c8.3-2 17.1-3.4 25.7-3.4 10.9 0 20.9 4.3 31.4 4.3 4.6 0 27.7-1.1 27.7-8v-60c0-2.6-2-4.6-4.6-4.6-5.1 0-15.1 4.3-24 4.3-9.7 0-20.9-4.3-32.6-4.3-8 0-16 1.1-23.7 2.9v-4.9c5.4-2.6 9.1-8.3 9.1-14.3 0-20.7-31.4-20.8-31.4 0 0 6 3.7 11.7 9.1 14.3v111.7c-3.7 0-11.7-1.4-11.7 4.6v32h-36.6v-32c0-2.6-2-4.6-4.6-4.6h-27.4c-2.6 0-4.6 2-4.6 4.6v32H128v-32c0-2.6-2-4.6-4.6-4.6H96c-2.6 0-4.6 2-4.6 4.6v178.3H54.8v-32c0-2.6-2-4.6-4.6-4.6H22.8c-2.6 0-4.6 2-4.6 4.6V512h182.9v-96c0-72.6 109.7-72.6 109.7 0v96h182.9V292.5c.1-2.6-1.9-4.6-4.5-4.6zm-288.1-4.5c0 2.6-2 4.6-4.6 4.6h-27.4c-2.6 0-4.6-2-4.6-4.6v-64c0-2.6 2-4.6 4.6-4.6h27.4c2.6 0 4.6 2 4.6 4.6v64zm146.4 0c0 2.6-2 4.6-4.6 4.6h-27.4c-2.6 0-4.6-2-4.6-4.6v-64c0-2.6 2-4.6 4.6-4.6h27.4c2.6 0 4.6 2 4.6 4.6v64z"/></svg>
  首页
</a></div><div class="nav-item"><a href="/blog-assets/tags/" class="nav-link ov-parent ov-hover"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path d="M0 252.118V48C0 21.49 21.49 0 48 0h204.118a48 48 0 0133.941 14.059l211.882 211.882c18.745 18.745 18.745 49.137 0 67.882L293.823 497.941c-18.745 18.745-49.137 18.745-67.882 0L14.059 286.059A48 48 0 010 252.118zM112 64c-26.51 0-48 21.49-48 48s21.49 48 48 48 48-21.49 48-48-21.49-48-48-48z"/></svg>
  标签
</a></div><div class="nav-item"><a href="/blog-assets/links/" class="nav-link ov-parent ov-hover"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path d="M305.45 462.59c7.391 7.298 6.188 20.097-3 25.004-77.714 41.802-176.726 29.91-242.344-35.709-65.602-65.603-77.51-164.523-35.692-242.331 4.891-9.095 17.69-10.298 25.003-3l116.812 116.813 27.394-27.394c-.688-2.61-1.594-5.001-1.594-7.814a32.004 32.004 0 1132.004 32.005c-2.797 0-5.204-.891-7.798-1.594l-27.41 27.41zm206.526-159.523a16.103 16.103 0 01-16.002 17.003H463.86a15.97 15.97 0 01-15.892-15.002C440.467 175.549 336.453 70.534 207.03 63.533a15.845 15.845 0 01-15.002-15.908V16.027A16.094 16.094 0 01209.03.024C372.255 8.62 503.475 139.841 511.976 303.067zm-96.012-.297a16.21 16.21 0 01-16.112 17.3h-32.207a16.069 16.069 0 01-15.893-14.705c-6.907-77.011-68.118-138.91-144.924-145.224a15.94 15.94 0 01-14.8-15.893v-32.114a16.134 16.134 0 0117.3-16.096c110.123 8.501 198.228 96.607 206.636 206.732z"/></svg>
  链接
</a></div><div class="nav-item"><a href="/blog-assets/docs/" aria-current="page" class="nav-link ov-parent ov-hover router-link-exact-active router-link-active"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="0 0 24 24" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path fill="none" d="M0 0h24v24H0z"/><path d="M21 18H6a1 1 0 000 2h15v2H6a3 3 0 01-3-3V4a2 2 0 012-2h16v16zm-5-9V7H8v2h8z"/></svg>
  文档
</a></div><div class="nav-item"><a href="https://github.com/mochen1942" target="_blank" rel="noopener noreferrer" class="nav-link external ov-parent ov-hover"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="0 0 512 512" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path d="M256 32C132.3 32 32 134.9 32 261.7c0 101.5 64.2 187.5 153.2 217.9a17.56 17.56 0 003.8.4c8.3 0 11.5-6.1 11.5-11.4 0-5.5-.2-19.9-.3-39.1a102.4 102.4 0 01-22.6 2.7c-43.1 0-52.9-33.5-52.9-33.5-10.2-26.5-24.9-33.6-24.9-33.6-19.5-13.7-.1-14.1 1.4-14.1h.1c22.5 2 34.3 23.8 34.3 23.8 11.2 19.6 26.2 25.1 39.6 25.1a63 63 0 0025.6-6c2-14.8 7.8-24.9 14.2-30.7-49.7-5.8-102-25.5-102-113.5 0-25.1 8.7-45.6 23-61.6-2.3-5.8-10-29.2 2.2-60.8a18.64 18.64 0 015-.5c8.1 0 26.4 3.1 56.6 24.1a208.21 208.21 0 01112.2 0c30.2-21 48.5-24.1 56.6-24.1a18.64 18.64 0 015 .5c12.2 31.6 4.5 55 2.2 60.8 14.3 16.1 23 36.6 23 61.6 0 88.2-52.4 107.6-102.3 113.3 8 7.1 15.2 21.1 15.2 42.5 0 30.7-.3 55.5-.3 63 0 5.4 3.1 11.5 11.4 11.5a19.35 19.35 0 004-.4C415.9 449.2 480 363.1 480 261.7 480 134.9 379.7 32 256 32z"/></svg>
  仓库地址
</a></div> <div class="nav-item"><a class="nav-link ov-parent ov-hover" style="cursor: pointer"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="0 0 24 24" fill="currentColor" class="ov-icon ov-wrench ov-hover" style="font-size:1.2em;"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 2c4.968 0 9 4.032 9 9s-4.032 9-9 9-9-4.032-9-9 4.032-9 9-9zm0 16c3.867 0 7-3.133 7-7 0-3.868-3.133-7-7-7-3.868 0-7 3.132-7 7 0 3.867 3.132 7 7 7zm8.485.071l2.829 2.828-1.415 1.415-2.828-2.829 1.414-1.414z"/></svg>
      搜索
    </a></div></nav> <ul class="sidebar-links"><li class="sidebar-header"><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>基础</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li class="sidebar-header"><a href="/blog-assets/docs/" aria-current="page" class="active sidebar-link">服务端配置</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog-assets/docs/#一、nginx-简介" class="sidebar-link">一、Nginx 简介</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#二、nginx-入门" class="sidebar-link">二、Nginx 入门</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#三、nginx-实战" class="sidebar-link">三、Nginx 实战</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog-assets/docs/#http-反向代理" class="sidebar-link">Http 反向代理</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#https-反向代理" class="sidebar-link">Https 反向代理</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#负载均衡" class="sidebar-link">负载均衡</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#负载均衡策略" class="sidebar-link" style="padding-left:3rem;">负载均衡策略</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#轮询" class="sidebar-link" style="padding-left:4rem;">轮询</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#加权轮询" class="sidebar-link" style="padding-left:4rem;">加权轮询</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#最少连接" class="sidebar-link" style="padding-left:4rem;">最少连接</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#加权最少连接" class="sidebar-link" style="padding-left:4rem;">加权最少连接</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#ip-hash" class="sidebar-link" style="padding-left:4rem;">IP Hash</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#普通-hash" class="sidebar-link" style="padding-left:4rem;">普通 Hash</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#网站有多个-webapp-的配置" class="sidebar-link">网站有多个 webapp 的配置</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#静态站点" class="sidebar-link">静态站点</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#搭建文件服务器" class="sidebar-link">搭建文件服务器</a></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#解决跨域" class="sidebar-link">解决跨域</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog-assets/docs/#资源" class="sidebar-link">资源</a></li></ul></li><li class="sidebar-header"><a href="/blog-assets/docs/basic/installation.html" class="sidebar-link">Installation</a></li></ul></section></li><li class="sidebar-header"><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>进阶</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li class="sidebar-header"><a href="/blog-assets/docs/advanced/features.html" class="sidebar-link">More Features</a></li></ul></section></li></ul> </aside> <div class="content" data-v-57e42542><main class="page"><div class="doc-header" data-v-7d40f932> <div class="header-content" data-v-7d40f932><!----> <h1 class="title" data-v-7d40f932>
      服务端配置
    </h1> <!----> <div class="icons" data-v-7d40f932><div class="icon" data-v-7d40f932><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-75.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon" style="font-size:1.2em;" data-v-7d40f932><path d="M313.6 304c-28.7 0-42.5 16-89.6 16-47.1 0-60.8-16-89.6-16C60.2 304 0 364.2 0 438.4V464c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48v-25.6c0-74.2-60.2-134.4-134.4-134.4zM400 464H48v-25.6c0-47.6 38.8-86.4 86.4-86.4 14.6 0 38.3 16 89.6 16 51.7 0 74.9-16 89.6-16 47.6 0 86.4 38.8 86.4 86.4V464zM224 288c79.5 0 144-64.5 144-144S303.5 0 224 0 80 64.5 80 144s64.5 144 144 144zm0-240c52.9 0 96 43.1 96 96s-43.1 96-96 96-96-43.1-96-96 43.1-96 96-96z"/></svg> <span data-v-7d40f932>墨陈</span></div> <div class="icon" data-v-7d40f932><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-75.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon" style="font-size:1.2em;" data-v-7d40f932><path d="M400 64h-48V12c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v52H160V12c0-6.6-5.4-12-12-12h-40c-6.6 0-12 5.4-12 12v52H48C21.5 64 0 85.5 0 112v352c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V112c0-26.5-21.5-48-48-48zm-6 400H54c-3.3 0-6-2.7-6-6V160h352v298c0 3.3-2.7 6-6 6z"/></svg> <span data-v-7d40f932>2022-10-18</span></div> <div class="icon" data-v-7d40f932><svg aria-hidden="true" width="19.2" height="19.2" viewBox="0 0 24 24" fill="currentColor" class="ov-icon" style="font-size:1.2em;" data-v-7d40f932><path fill="none" d="M0 0h24v24H0z"/><path d="M17.618 5.968l1.453-1.453 1.414 1.414-1.453 1.453a9 9 0 11-1.414-1.414zM12 20a7 7 0 100-14 7 7 0 000 14zM11 8h2v6h-2V8zM8 1h8v2H8V1z"/></svg> <span data-v-7d40f932>17 min</span></div></div></div></div> <div class="theme-content content__default"><h1 id="nginx-极简教程"><a href="#nginx-极简教程" class="header-anchor">#</a> Nginx 极简教程</h1> <blockquote><p>本项目是一个 Nginx 极简教程，目的在于帮助新手快速入门 Nginx。</p> <p><a href="https://github.com/dunwu/nginx-tutorial/tree/master/examples" target="_blank" rel="noopener noreferrer"><strong>examples</strong></a> 目录中的示例模拟了工作中的一些常用实战场景，并且都可以通过脚本一键式启动，让您可以快速看到演示效果。</p></blockquote> <ul><li><a href="#%E4%B8%80nginx-%E7%AE%80%E4%BB%8B">一、Nginx 简介</a></li> <li><a href="#%E4%BA%8Cnginx-%E5%85%A5%E9%97%A8">二、Nginx 入门</a></li> <li><a href="#%E4%B8%89nginx-%E5%AE%9E%E6%88%98">三、Nginx 实战</a> <ul><li><a href="#http-%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86">Http 反向代理</a></li> <li><a href="#https-%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86">Https 反向代理</a></li> <li><a href="#%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1">负载均衡</a></li> <li><a href="#%E7%BD%91%E7%AB%99%E6%9C%89%E5%A4%9A%E4%B8%AA-webapp-%E7%9A%84%E9%85%8D%E7%BD%AE">网站有多个 webapp 的配置</a></li> <li><a href="#%E9%9D%99%E6%80%81%E7%AB%99%E7%82%B9">静态站点</a></li> <li><a href="#%E6%90%AD%E5%BB%BA%E6%96%87%E4%BB%B6%E6%9C%8D%E5%8A%A1%E5%99%A8">搭建文件服务器</a></li> <li><a href="#%E8%A7%A3%E5%86%B3%E8%B7%A8%E5%9F%9F">解决跨域</a></li></ul></li> <li><a href="#%E8%B5%84%E6%BA%90">资源</a></li></ul> <h2 id="一、nginx-简介"><a href="#一、nginx-简介" class="header-anchor">#</a> 一、Nginx 简介</h2> <p><strong>什么是 Nginx?</strong></p> <p><strong>Nginx (engine x)</strong> 是一款轻量级的 Web 服务器 、反向代理服务器及电子邮件（IMAP/POP3）代理服务器。</p> <img src="/blog-assets/img/docs/nginx.jpg"> <p><strong>什么是反向代理？</strong></p> <p>反向代理（Reverse Proxy）方式是指以代理服务器来接受 internet 上的连接请求，然后将请求转发给内部网络上的服务器，并将从服务器上得到的结果返回给 internet 上请求连接的客户端，此时代理服务器对外就表现为一个反向代理服务器。</p> <img src="/blog-assets/img/docs/reverse-proxy.png"> <h2 id="二、nginx-入门"><a href="#二、nginx-入门" class="header-anchor">#</a> 二、Nginx 入门</h2> <blockquote><p>详细安装方法请参考：<a href="/blog-assets/docs/docs/nginx-ops.html">Nginx 运维</a></p></blockquote> <p>nginx 的使用比较简单，就是几条命令。</p> <p>常用到的命令如下：</p> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token command"><span class="token keyword">nginx</span> <span class="token parameter attr-name">-s</span> stop       快速关闭Nginx，可能不保存相关信息，并迅速终止web服务。</span>
<span class="token command"><span class="token keyword">nginx</span> <span class="token parameter attr-name">-s</span> quit       平稳关闭Nginx，保存相关信息，有安排的结束web服务。</span>
<span class="token command"><span class="token keyword">nginx</span> <span class="token parameter attr-name">-s</span> reload     因改变了Nginx相关配置，需要重新加载配置而重载。</span>
<span class="token command"><span class="token keyword">nginx</span> <span class="token parameter attr-name">-s</span> reopen     重新打开日志文件。</span>
<span class="token command"><span class="token keyword">nginx</span> <span class="token parameter attr-name">-c</span> filename   为 Nginx 指定一个配置文件，来代替缺省的。</span>
<span class="token command"><span class="token keyword">nginx</span> <span class="token parameter attr-name">-t</span>            不运行，仅仅测试配置文件。nginx 将检查配置文件的语法的正确性，并尝试打开配置文件中所引用到的文件。</span>
<span class="token command"><span class="token keyword">nginx</span> <span class="token parameter attr-name">-v</span>            显示 nginx 的版本。</span>
<span class="token command"><span class="token keyword">nginx</span> <span class="token parameter attr-name">-V</span>            显示 nginx 的版本，编译器版本和配置参数。</span>
</code></pre></div><p>如果不想每次都敲命令，可以在 nginx 安装目录下新添一个启动批处理文件<strong>startup.bat</strong>，双击即可运行。内容如下：</p> <div class="language-batch extra-class"><pre class="language-batch"><code><span class="token operator">@</span><span class="token command"><span class="token keyword">echo</span> off</span>
<span class="token comment">rem 如果启动前已经启动nginx并记录下pid文件，会kill指定进程</span>
<span class="token command"><span class="token keyword">nginx</span>.exe <span class="token parameter attr-name">-s</span> stop</span>

<span class="token comment">rem 测试配置文件语法正确性</span>
<span class="token command"><span class="token keyword">nginx</span>.exe <span class="token parameter attr-name">-t</span> <span class="token parameter attr-name">-c</span> conf/nginx.conf</span>

<span class="token comment">rem 显示版本信息</span>
<span class="token command"><span class="token keyword">nginx</span>.exe <span class="token parameter attr-name">-v</span></span>

<span class="token comment">rem 按照指定配置去启动nginx</span>
<span class="token command"><span class="token keyword">nginx</span>.exe <span class="token parameter attr-name">-c</span> conf/nginx.conf</span>
</code></pre></div><p>如果是运行在 Linux 下，写一个 shell 脚本，大同小异。</p> <h2 id="三、nginx-实战"><a href="#三、nginx-实战" class="header-anchor">#</a> 三、Nginx 实战</h2> <p>我始终认为，各种开发工具的配置还是结合实战来讲述，会让人更易理解。</p> <h3 id="http-反向代理"><a href="#http-反向代理" class="header-anchor">#</a> Http 反向代理</h3> <p>我们先实现一个小目标：不考虑复杂的配置，仅仅是完成一个 http 反向代理。</p> <p><code>nginx.conf</code> 配置文件如下：</p> <blockquote><p><em><strong>注：<code>conf/nginx.conf</code> 是 nginx 的默认配置文件。你也可以使用 nginx -c 指定你的配置文件</strong></em></p></blockquote> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token comment">#运行用户</span>
<span class="token comment">#user somebody;</span>

<span class="token comment">#启动进程,通常设置成和cpu的数量相等</span>
<span class="token directive"><span class="token keyword">worker_processes</span>  <span class="token number">1</span></span><span class="token punctuation">;</span>

<span class="token comment">#全局错误日志</span>
<span class="token directive"><span class="token keyword">error_log</span>  D:/Tools/nginx-1.10.1/logs/error.log</span><span class="token punctuation">;</span>
<span class="token directive"><span class="token keyword">error_log</span>  D:/Tools/nginx-1.10.1/logs/notice.log  notice</span><span class="token punctuation">;</span>
<span class="token directive"><span class="token keyword">error_log</span>  D:/Tools/nginx-1.10.1/logs/info.log  info</span><span class="token punctuation">;</span>

<span class="token comment">#PID文件，记录当前启动的nginx的进程ID</span>
<span class="token directive"><span class="token keyword">pid</span>        D:/Tools/nginx-1.10.1/logs/nginx.pid</span><span class="token punctuation">;</span>

<span class="token comment">#工作模式及连接数上限</span>
<span class="token directive"><span class="token keyword">events</span></span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword">worker_connections</span> <span class="token number">1024</span></span><span class="token punctuation">;</span>    <span class="token comment">#单个后台worker process进程的最大并发链接数</span>
<span class="token punctuation">}</span>

<span class="token comment">#设定http服务器，利用它的反向代理功能提供负载均衡支持</span>
<span class="token directive"><span class="token keyword">http</span></span> <span class="token punctuation">{</span>
    <span class="token comment">#设定mime类型(邮件支持类型),类型由mime.types文件定义</span>
    <span class="token directive"><span class="token keyword">include</span>       D:/Tools/nginx-1.10.1/conf/mime.types</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">default_type</span>  application/octet-stream</span><span class="token punctuation">;</span>

    <span class="token comment">#设定日志</span>
	<span class="token directive"><span class="token keyword">log_format</span>  main  <span class="token string">'[<span class="token variable">$remote_addr]</span> - [<span class="token variable">$remote_user]</span> [<span class="token variable">$time_local]</span> &quot;<span class="token variable">$request</span>&quot; '</span>
                      <span class="token string">'<span class="token variable">$status</span> <span class="token variable">$body_bytes_sent</span> &quot;<span class="token variable">$http_referer</span>&quot; '</span>
                      <span class="token string">'&quot;<span class="token variable">$http_user_agent</span>&quot; &quot;<span class="token variable">$http_x_forwarded_for</span>&quot;'</span></span><span class="token punctuation">;</span>

    <span class="token directive"><span class="token keyword">access_log</span>    D:/Tools/nginx-1.10.1/logs/access.log main</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">rewrite_log</span>     <span class="token boolean">on</span></span><span class="token punctuation">;</span>

    <span class="token comment">#sendfile 指令指定 nginx 是否调用 sendfile 函数（zero copy 方式）来输出文件，对于普通应用，</span>
    <span class="token comment">#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用，可设置为 off，以平衡磁盘与网络I/O处理速度，降低系统的uptime.</span>
    <span class="token directive"><span class="token keyword">sendfile</span>        <span class="token boolean">on</span></span><span class="token punctuation">;</span>
    <span class="token comment">#tcp_nopush     on;</span>

    <span class="token comment">#连接超时时间</span>
    <span class="token directive"><span class="token keyword">keepalive_timeout</span>  <span class="token number">120</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">tcp_nodelay</span>        <span class="token boolean">on</span></span><span class="token punctuation">;</span>

	<span class="token comment">#gzip压缩开关</span>
	<span class="token comment">#gzip  on;</span>

    <span class="token comment">#设定实际的服务器列表</span>
    <span class="token directive"><span class="token keyword">upstream</span> zp_server1</span><span class="token punctuation">{</span>
        <span class="token directive"><span class="token keyword">server</span> 127.0.0.1:8089</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">#HTTP服务器</span>
    <span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span>
        <span class="token comment">#监听80端口，80端口是知名端口号，用于HTTP协议</span>
        <span class="token directive"><span class="token keyword">listen</span>       <span class="token number">80</span></span><span class="token punctuation">;</span>

        <span class="token comment">#定义使用www.xx.com访问</span>
        <span class="token directive"><span class="token keyword">server_name</span>  www.helloworld.com</span><span class="token punctuation">;</span>

		<span class="token comment">#首页</span>
		<span class="token directive"><span class="token keyword">index</span> index.html

		<span class="token comment">#指向webapp的目录</span>
		root D:\01_Workspace\Project\github\zp\SpringNotes\spring-security\spring-shiro\src\main\webapp</span><span class="token punctuation">;</span>

		<span class="token comment">#编码格式</span>
		<span class="token directive"><span class="token keyword">charset</span> utf-8</span><span class="token punctuation">;</span>

		<span class="token comment">#代理配置参数</span>
        <span class="token directive"><span class="token keyword">proxy_connect_timeout</span> <span class="token number">180</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword">proxy_send_timeout</span> <span class="token number">180</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword">proxy_read_timeout</span> <span class="token number">180</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword">proxy_set_header</span> X-Forwarder-For <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>

        <span class="token comment">#反向代理的路径（和upstream绑定），location 后面设置映射的路径</span>
        <span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span>
            <span class="token directive"><span class="token keyword">proxy_pass</span> http://zp_server1</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">#静态文件，nginx自己处理</span>
        <span class="token directive"><span class="token keyword">location</span> ~ ^/(images|javascript|js|css|flash|media|static)/</span> <span class="token punctuation">{</span>
            <span class="token directive"><span class="token keyword">root</span> D:\01_Workspace\Project\github\zp\SpringNotes\spring-security\spring-shiro\src\main\webapp\views</span><span class="token punctuation">;</span>
            <span class="token comment">#过期30天，静态文件不怎么更新，过期可以设大一点，如果频繁更新，则可以设置得小一点。</span>
            <span class="token directive"><span class="token keyword">expires</span> <span class="token number">30d</span></span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">#设定查看Nginx状态的地址</span>
        <span class="token directive"><span class="token keyword">location</span> /NginxStatus</span> <span class="token punctuation">{</span>
            <span class="token directive"><span class="token keyword">stub_status</span>           <span class="token boolean">on</span></span><span class="token punctuation">;</span>
            <span class="token directive"><span class="token keyword">access_log</span>            <span class="token boolean">on</span></span><span class="token punctuation">;</span>
            <span class="token directive"><span class="token keyword">auth_basic</span>            <span class="token string">&quot;NginxStatus&quot;</span></span><span class="token punctuation">;</span>
            <span class="token directive"><span class="token keyword">auth_basic_user_file</span>  conf/htpasswd</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">#禁止访问 .htxxx 文件</span>
        <span class="token directive"><span class="token keyword">location</span> ~ /\.ht</span> <span class="token punctuation">{</span>
            <span class="token directive"><span class="token keyword">deny</span> all</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

		<span class="token comment">#错误处理页面（可选择性配置）</span>
		<span class="token comment">#error_page   404              /404.html;</span>
		<span class="token comment">#error_page   500 502 503 504  /50x.html;</span>
        <span class="token comment">#location = /50x.html {</span>
        <span class="token comment">#    root   html;</span>
        <span class="token comment">#}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>好了，让我们来试试吧：</p> <ol><li>启动 webapp，注意启动绑定的端口要和 nginx 中的 <code>upstream</code> 设置的端口保持一致。</li> <li>更改 host：在 C:\Windows\System32\drivers\etc 目录下的 host 文件中添加一条 DNS 记录</li></ol> <div class="language- extra-class"><pre class="language-text"><code>127.0.0.1 www.helloworld.com
</code></pre></div><ol start="3"><li>启动前文中 startup.bat 的命令</li> <li>在浏览器中访问 www.helloworld.com，不出意外，已经可以访问了。</li></ol> <h3 id="https-反向代理"><a href="#https-反向代理" class="header-anchor">#</a> Https 反向代理</h3> <p>一些对安全性要求比较高的站点，可能会使用 HTTPS（一种使用 ssl 通信标准的安全 HTTP 协议）。</p> <p>这里不科普 HTTP 协议和 SSL 标准。但是，使用 nginx 配置 https 需要知道几点：</p> <ul><li>HTTPS 的固定端口号是 443，不同于 HTTP 的 80 端口</li> <li>SSL 标准需要引入安全证书，所以在 nginx.conf 中你需要指定证书和它对应的 key</li></ul> <p>其他和 http 反向代理基本一样，只是在 <code>Server</code> 部分配置有些不同。</p> <div class="language-nginx extra-class"><pre class="language-nginx"><code>  <span class="token comment">#HTTP服务器</span>
  <span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span>
      <span class="token comment">#监听443端口。443为知名端口号，主要用于HTTPS协议</span>
      <span class="token directive"><span class="token keyword">listen</span>       <span class="token number">443</span> ssl</span><span class="token punctuation">;</span>

      <span class="token comment">#定义使用www.xx.com访问</span>
      <span class="token directive"><span class="token keyword">server_name</span>  www.helloworld.com</span><span class="token punctuation">;</span>

      <span class="token comment">#ssl证书文件位置(常见证书文件格式为：crt/pem)</span>
      <span class="token directive"><span class="token keyword">ssl_certificate</span>      cert.pem</span><span class="token punctuation">;</span>
      <span class="token comment">#ssl证书key位置</span>
      <span class="token directive"><span class="token keyword">ssl_certificate_key</span>  cert.key</span><span class="token punctuation">;</span>

      <span class="token comment">#ssl配置参数（选择性配置）</span>
      <span class="token directive"><span class="token keyword">ssl_session_cache</span>    shared:SSL:1m</span><span class="token punctuation">;</span>
      <span class="token directive"><span class="token keyword">ssl_session_timeout</span>  <span class="token number">5m</span></span><span class="token punctuation">;</span>
      <span class="token comment">#数字签名，此处使用MD5</span>
      <span class="token directive"><span class="token keyword">ssl_ciphers</span>  HIGH:!aNULL:!MD5</span><span class="token punctuation">;</span>
      <span class="token directive"><span class="token keyword">ssl_prefer_server_ciphers</span>  <span class="token boolean">on</span></span><span class="token punctuation">;</span>

      <span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span>
          <span class="token directive"><span class="token keyword">root</span>   /root</span><span class="token punctuation">;</span>
          <span class="token directive"><span class="token keyword">index</span>  index.html index.htm</span><span class="token punctuation">;</span>
      <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
</code></pre></div><h3 id="负载均衡"><a href="#负载均衡" class="header-anchor">#</a> 负载均衡</h3> <p>前面的例子中，代理仅仅指向一个服务器。</p> <p>但是，网站在实际运营过程中，大部分都是以集群的方式运行，这时需要使用负载均衡来分流。</p> <p>nginx 也可以实现简单的负载均衡功能。</p> <img src="/blog-assets/img/docs/nginx-load-balance.png"> <p>假设这样一个应用场景：将应用部署在 192.168.1.11:80、192.168.1.12:80、192.168.1.13:80 三台 linux 环境的服务器上。网站域名叫 www.helloworld.com，公网 IP 为 192.168.1.11。在公网 IP 所在的服务器上部署 nginx，对所有请求做负载均衡处理（下面例子中使用的是加权轮询策略）。</p> <p>nginx.conf 配置如下：</p> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">http</span></span> <span class="token punctuation">{</span>
     <span class="token comment">#设定mime类型,类型由mime.type文件定义</span>
    <span class="token directive"><span class="token keyword">include</span>       /etc/nginx/mime.types</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">default_type</span>  application/octet-stream</span><span class="token punctuation">;</span>
    <span class="token comment">#设定日志格式</span>
    <span class="token directive"><span class="token keyword">access_log</span>    /var/log/nginx/access.log</span><span class="token punctuation">;</span>

    <span class="token comment">#设定负载均衡的服务器列表</span>
    <span class="token directive"><span class="token keyword">upstream</span> load_balance_server</span> <span class="token punctuation">{</span>
        <span class="token comment">#weigth参数表示权值，权值越高被分配到的几率越大</span>
        <span class="token directive"><span class="token keyword">server</span> 192.168.1.11:80   weight=5</span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword">server</span> 192.168.1.12:80   weight=1</span><span class="token punctuation">;</span>
        <span class="token directive"><span class="token keyword">server</span> 192.168.1.13:80   weight=6</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

   <span class="token comment">#HTTP服务器</span>
   <span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span>
        <span class="token comment">#侦听80端口</span>
        <span class="token directive"><span class="token keyword">listen</span>       <span class="token number">80</span></span><span class="token punctuation">;</span>

        <span class="token comment">#定义使用www.xx.com访问</span>
        <span class="token directive"><span class="token keyword">server_name</span>  www.helloworld.com</span><span class="token punctuation">;</span>

        <span class="token comment">#对所有请求进行负载均衡请求</span>
        <span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span>
            <span class="token directive"><span class="token keyword">root</span>        /root</span><span class="token punctuation">;</span>                 <span class="token comment">#定义服务器的默认网站根目录位置</span>
            <span class="token directive"><span class="token keyword">index</span>       index.html index.htm</span><span class="token punctuation">;</span>  <span class="token comment">#定义首页索引文件的名称</span>
            <span class="token directive"><span class="token keyword">proxy_pass</span>  http://load_balance_server</span> <span class="token punctuation">;</span><span class="token comment">#请求转向load_balance_server 定义的服务器列表</span>

            <span class="token comment">#以下是一些反向代理的配置(可选择性配置)</span>
            <span class="token comment">#proxy_redirect off;</span>
            <span class="token directive"><span class="token keyword">proxy_set_header</span> Host <span class="token variable">$host</span></span><span class="token punctuation">;</span>
            <span class="token directive"><span class="token keyword">proxy_set_header</span> X-Real-IP <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
            <span class="token comment">#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP</span>
            <span class="token directive"><span class="token keyword">proxy_set_header</span> X-Forwarded-For <span class="token variable">$remote_addr</span></span><span class="token punctuation">;</span>
            <span class="token directive"><span class="token keyword">proxy_connect_timeout</span> <span class="token number">90</span></span><span class="token punctuation">;</span>          <span class="token comment">#nginx跟后端服务器连接超时时间(代理连接超时)</span>
            <span class="token directive"><span class="token keyword">proxy_send_timeout</span> <span class="token number">90</span></span><span class="token punctuation">;</span>             <span class="token comment">#后端服务器数据回传时间(代理发送超时)</span>
            <span class="token directive"><span class="token keyword">proxy_read_timeout</span> <span class="token number">90</span></span><span class="token punctuation">;</span>             <span class="token comment">#连接成功后，后端服务器响应时间(代理接收超时)</span>
            <span class="token directive"><span class="token keyword">proxy_buffer_size</span> <span class="token number">4k</span></span><span class="token punctuation">;</span>              <span class="token comment">#设置代理服务器（nginx）保存用户头信息的缓冲区大小</span>
            <span class="token directive"><span class="token keyword">proxy_buffers</span> <span class="token number">4</span> <span class="token number">32k</span></span><span class="token punctuation">;</span>               <span class="token comment">#proxy_buffers缓冲区，网页平均在32k以下的话，这样设置</span>
            <span class="token directive"><span class="token keyword">proxy_busy_buffers_size</span> <span class="token number">64k</span></span><span class="token punctuation">;</span>       <span class="token comment">#高负荷下缓冲大小（proxy_buffers*2）</span>
            <span class="token directive"><span class="token keyword">proxy_temp_file_write_size</span> <span class="token number">64k</span></span><span class="token punctuation">;</span>    <span class="token comment">#设定缓存文件夹大小，大于这个值，将从upstream服务器传</span>

            <span class="token directive"><span class="token keyword">client_max_body_size</span> <span class="token number">10m</span></span><span class="token punctuation">;</span>          <span class="token comment">#允许客户端请求的最大单文件字节数</span>
            <span class="token directive"><span class="token keyword">client_body_buffer_size</span> <span class="token number">128k</span></span><span class="token punctuation">;</span>      <span class="token comment">#缓冲区代理缓冲用户端请求的最大字节数</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h4 id="负载均衡策略"><a href="#负载均衡策略" class="header-anchor">#</a> 负载均衡策略</h4> <p>Nginx 提供了多种负载均衡策略，让我们来一一了解一下：</p> <p>负载均衡策略在各种分布式系统中基本上原理一致，对于原理有兴趣，不妨参考 <a href="https://dunwu.github.io/blog/design/theory/load-balance-theory/" target="_blank" rel="noopener noreferrer">负载均衡</a></p> <h5 id="轮询"><a href="#轮询" class="header-anchor">#</a> 轮询</h5> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">upstream</span> bck_testing_01</span> <span class="token punctuation">{</span>
  <span class="token comment"># 默认所有服务器权重为 1</span>
  server 192.168.250.220:8080
  server 192.168.250.221:8080
  server 192.168.250.222:8080
<span class="token punctuation">}</span>
</code></pre></div><h5 id="加权轮询"><a href="#加权轮询" class="header-anchor">#</a> 加权轮询</h5> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">upstream</span> bck_testing_01</span> <span class="token punctuation">{</span>
  server 192.168.250.220:8080   weight=3
  server 192.168.250.221:8080              <span class="token comment"># default weight=1</span>
  server 192.168.250.222:8080              <span class="token comment"># default weight=1</span>
<span class="token punctuation">}</span>
</code></pre></div><h5 id="最少连接"><a href="#最少连接" class="header-anchor">#</a> 最少连接</h5> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">upstream</span> bck_testing_01</span> <span class="token punctuation">{</span>
  <span class="token directive"><span class="token keyword">least_conn</span></span><span class="token punctuation">;</span>

  <span class="token comment"># with default weight for all (weight=1)</span>
  server 192.168.250.220:8080
  server 192.168.250.221:8080
  server 192.168.250.222:8080
<span class="token punctuation">}</span>
</code></pre></div><h5 id="加权最少连接"><a href="#加权最少连接" class="header-anchor">#</a> 加权最少连接</h5> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">upstream</span> bck_testing_01</span> <span class="token punctuation">{</span>
  <span class="token directive"><span class="token keyword">least_conn</span></span><span class="token punctuation">;</span>

  server 192.168.250.220:8080   weight=3
  server 192.168.250.221:8080              <span class="token comment"># default weight=1</span>
  server 192.168.250.222:8080              <span class="token comment"># default weight=1</span>
<span class="token punctuation">}</span>
</code></pre></div><h5 id="ip-hash"><a href="#ip-hash" class="header-anchor">#</a> IP Hash</h5> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">upstream</span> bck_testing_01</span> <span class="token punctuation">{</span>

  <span class="token directive"><span class="token keyword">ip_hash</span></span><span class="token punctuation">;</span>

  <span class="token comment"># with default weight for all (weight=1)</span>
  server 192.168.250.220:8080
  server 192.168.250.221:8080
  server 192.168.250.222:8080

<span class="token punctuation">}</span>
</code></pre></div><h5 id="普通-hash"><a href="#普通-hash" class="header-anchor">#</a> 普通 Hash</h5> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">upstream</span> bck_testing_01</span> <span class="token punctuation">{</span>

  <span class="token directive"><span class="token keyword">hash</span> <span class="token variable">$request_uri</span></span><span class="token punctuation">;</span>

  <span class="token comment"># with default weight for all (weight=1)</span>
  server 192.168.250.220:8080
  server 192.168.250.221:8080
  server 192.168.250.222:8080

<span class="token punctuation">}</span>
</code></pre></div><h3 id="网站有多个-webapp-的配置"><a href="#网站有多个-webapp-的配置" class="header-anchor">#</a> 网站有多个 webapp 的配置</h3> <p>当一个网站功能越来越丰富时，往往需要将一些功能相对独立的模块剥离出来，独立维护。这样的话，通常，会有多个 webapp。</p> <p>举个例子：假如 www.helloworld.com 站点有好几个 webapp，finance（金融）、product（产品）、admin（用户中心）。访问这些应用的方式通过上下文(context)来进行区分:</p> <p>www.helloworld.com/finance/</p> <p>www.helloworld.com/product/</p> <p>www.helloworld.com/admin/</p> <p>我们知道，http 的默认端口号是 80，如果在一台服务器上同时启动这 3 个 webapp 应用，都用 80 端口，肯定是不成的。所以，这三个应用需要分别绑定不同的端口号。</p> <p>那么，问题来了，用户在实际访问 www.helloworld.com 站点时，访问不同 webapp，总不会还带着对应的端口号去访问吧。所以，你再次需要用到反向代理来做处理。</p> <p>配置也不难，来看看怎么做吧：</p> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">http</span></span> <span class="token punctuation">{</span>
	<span class="token comment">#此处省略一些基本配置</span>

	<span class="token directive"><span class="token keyword">upstream</span> product_server</span><span class="token punctuation">{</span>
		<span class="token directive"><span class="token keyword">server</span> www.helloworld.com:8081</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token directive"><span class="token keyword">upstream</span> admin_server</span><span class="token punctuation">{</span>
		<span class="token directive"><span class="token keyword">server</span> www.helloworld.com:8082</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token directive"><span class="token keyword">upstream</span> finance_server</span><span class="token punctuation">{</span>
		<span class="token directive"><span class="token keyword">server</span> www.helloworld.com:8083</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span>
		<span class="token comment">#此处省略一些基本配置</span>
		<span class="token comment">#默认指向product的server</span>
		<span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span>
			<span class="token directive"><span class="token keyword">proxy_pass</span> http://product_server</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>

		<span class="token directive"><span class="token keyword">location</span> /product/</span><span class="token punctuation">{</span>
			<span class="token directive"><span class="token keyword">proxy_pass</span> http://product_server</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>

		<span class="token directive"><span class="token keyword">location</span> /admin/</span> <span class="token punctuation">{</span>
			<span class="token directive"><span class="token keyword">proxy_pass</span> http://admin_server</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>

		<span class="token directive"><span class="token keyword">location</span> /finance/</span> <span class="token punctuation">{</span>
			<span class="token directive"><span class="token keyword">proxy_pass</span> http://finance_server</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="静态站点"><a href="#静态站点" class="header-anchor">#</a> 静态站点</h3> <p>有时候，我们需要配置静态站点(即 html 文件和一堆静态资源)。</p> <p>举例来说：如果所有的静态资源都放在了 <code>/app/dist</code> 目录下，我们只需要在 <code>nginx.conf</code> 中指定首页以及这个站点的 host 即可。</p> <p>配置如下：</p> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">worker_processes</span>  <span class="token number">1</span></span><span class="token punctuation">;</span>

<span class="token directive"><span class="token keyword">events</span></span> <span class="token punctuation">{</span>
	<span class="token directive"><span class="token keyword">worker_connections</span>  <span class="token number">1024</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token directive"><span class="token keyword">http</span></span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword">include</span>       mime.types</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">default_type</span>  application/octet-stream</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">sendfile</span>        <span class="token boolean">on</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">keepalive_timeout</span>  <span class="token number">65</span></span><span class="token punctuation">;</span>

    <span class="token directive"><span class="token keyword">gzip</span> <span class="token boolean">on</span></span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">gzip_types</span> text/plain application/x-javascript text/css application/xml text/javascript application/javascript image/jpeg image/gif image/png</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">gzip_vary</span> <span class="token boolean">on</span></span><span class="token punctuation">;</span>

    <span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span>
		<span class="token directive"><span class="token keyword">listen</span>       <span class="token number">80</span></span><span class="token punctuation">;</span>
		<span class="token directive"><span class="token keyword">server_name</span>  static.zp.cn</span><span class="token punctuation">;</span>

		<span class="token directive"><span class="token keyword">location</span> /</span> <span class="token punctuation">{</span>
			<span class="token directive"><span class="token keyword">root</span> /app/dist</span><span class="token punctuation">;</span>
			<span class="token directive"><span class="token keyword">index</span> index.html</span><span class="token punctuation">;</span>
			<span class="token comment">#转发任何请求到 index.html</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>然后，添加 HOST：</p> <p>127.0.0.1 static.zp.cn</p> <p>此时，在本地浏览器访问 static.zp.cn ，就可以访问静态站点了。</p> <h3 id="搭建文件服务器"><a href="#搭建文件服务器" class="header-anchor">#</a> 搭建文件服务器</h3> <p>有时候，团队需要归档一些数据或资料，那么文件服务器必不可少。使用 Nginx 可以非常快速便捷的搭建一个简易的文件服务。</p> <p>Nginx 中的配置要点：</p> <ul><li>将 autoindex 开启可以显示目录，默认不开启。</li> <li>将 autoindex_exact_size 开启可以显示文件的大小。</li> <li>将 autoindex_localtime 开启可以显示文件的修改时间。</li> <li>root 用来设置开放为文件服务的根路径。</li> <li>charset 设置为 <code>charset utf-8,gbk;</code>，可以避免中文乱码问题（windows 服务器下设置后，依然乱码，本人暂时没有找到解决方法）。</li></ul> <p>一个最简化的配置如下：</p> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token directive"><span class="token keyword">autoindex</span> <span class="token boolean">on</span></span><span class="token punctuation">;</span><span class="token comment"># 显示目录</span>
<span class="token directive"><span class="token keyword">autoindex_exact_size</span> <span class="token boolean">on</span></span><span class="token punctuation">;</span><span class="token comment"># 显示文件大小</span>
<span class="token directive"><span class="token keyword">autoindex_localtime</span> <span class="token boolean">on</span></span><span class="token punctuation">;</span><span class="token comment"># 显示文件时间</span>

<span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword">charset</span>      utf-8,gbk</span><span class="token punctuation">;</span> <span class="token comment"># windows 服务器下设置后，依然乱码，暂时无解</span>
    <span class="token directive"><span class="token keyword">listen</span>       <span class="token number">9050</span> default_server</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">listen</span>       [::]:9050 default_server</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">server_name</span>  _</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">root</span>         /share/fs</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="解决跨域"><a href="#解决跨域" class="header-anchor">#</a> 解决跨域</h3> <p>web 领域开发中，经常采用前后端分离模式。这种模式下，前端和后端分别是独立的 web 应用程序，例如：后端是 Java 程序，前端是 React 或 Vue 应用。</p> <p>各自独立的 web app 在互相访问时，势必存在跨域问题。解决跨域问题一般有两种思路：</p> <ol><li><strong>CORS</strong></li></ol> <p>在后端服务器设置 HTTP 响应头，把你需要允许访问的域名加入 <code>Access-Control-Allow-Origin</code> 中。</p> <ol start="2"><li><strong>jsonp</strong></li></ol> <p>把后端根据请求，构造 json 数据，并返回，前端用 jsonp 跨域。</p> <p>这两种思路，本文不展开讨论。</p> <p>需要说明的是，nginx 根据第一种思路，也提供了一种解决跨域的解决方案。</p> <p>举例：www.helloworld.com 网站是由一个前端 app ，一个后端 app 组成的。前端端口号为 9000， 后端端口号为 8080。</p> <p>前端和后端如果使用 http 进行交互时，请求会被拒绝，因为存在跨域问题。来看看，nginx 是怎么解决的吧：</p> <p>首先，在 enable-cors.conf 文件中设置 cors ：</p> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token comment"># allow origin list</span>
<span class="token directive"><span class="token keyword">set</span> <span class="token variable">$ACAO</span> <span class="token string">'*'</span></span><span class="token punctuation">;</span>

<span class="token comment"># set single origin</span>
<span class="token directive"><span class="token keyword">if</span> (<span class="token variable">$http_origin</span> ~* (www.helloworld.com)$)</span> <span class="token punctuation">{</span>
  <span class="token directive"><span class="token keyword">set</span> <span class="token variable">$ACAO</span> <span class="token variable">$http_origin</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token directive"><span class="token keyword">if</span> (<span class="token variable">$cors</span> = <span class="token string">&quot;trueget&quot;</span>)</span> <span class="token punctuation">{</span>
	<span class="token directive"><span class="token keyword">add_header</span> <span class="token string">'Access-Control-Allow-Origin'</span> <span class="token string">&quot;<span class="token variable">$http_origin</span>&quot;</span></span><span class="token punctuation">;</span>
	<span class="token directive"><span class="token keyword">add_header</span> <span class="token string">'Access-Control-Allow-Credentials'</span> <span class="token string">'true'</span></span><span class="token punctuation">;</span>
	<span class="token directive"><span class="token keyword">add_header</span> <span class="token string">'Access-Control-Allow-Methods'</span> <span class="token string">'GET, POST, OPTIONS'</span></span><span class="token punctuation">;</span>
	<span class="token directive"><span class="token keyword">add_header</span> <span class="token string">'Access-Control-Allow-Headers'</span> <span class="token string">'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token directive"><span class="token keyword">if</span> (<span class="token variable">$request_method</span> = <span class="token string">'OPTIONS'</span>)</span> <span class="token punctuation">{</span>
  <span class="token directive"><span class="token keyword">set</span> <span class="token variable">$cors</span> <span class="token string">&quot;<span class="token variable">${cors}</span>options&quot;</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token directive"><span class="token keyword">if</span> (<span class="token variable">$request_method</span> = <span class="token string">'GET'</span>)</span> <span class="token punctuation">{</span>
  <span class="token directive"><span class="token keyword">set</span> <span class="token variable">$cors</span> <span class="token string">&quot;<span class="token variable">${cors}</span>get&quot;</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token directive"><span class="token keyword">if</span> (<span class="token variable">$request_method</span> = <span class="token string">'POST'</span>)</span> <span class="token punctuation">{</span>
  <span class="token directive"><span class="token keyword">set</span> <span class="token variable">$cors</span> <span class="token string">&quot;<span class="token variable">${cors}</span>post&quot;</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>接下来，在你的服务器中 <code>include enable-cors.conf</code> 来引入跨域配置：</p> <div class="language-nginx extra-class"><pre class="language-nginx"><code><span class="token comment"># ----------------------------------------------------</span>
<span class="token comment"># 此文件为项目 nginx 配置片段</span>
<span class="token comment"># 可以直接在 nginx config 中 include（推荐）</span>
<span class="token comment"># 或者 copy 到现有 nginx 中，自行配置</span>
<span class="token comment"># www.helloworld.com 域名需配合 dns hosts 进行配置</span>
<span class="token comment"># 其中，api 开启了 cors，需配合本目录下另一份配置文件</span>
<span class="token comment"># ----------------------------------------------------</span>
<span class="token directive"><span class="token keyword">upstream</span> front_server</span><span class="token punctuation">{</span>
  <span class="token directive"><span class="token keyword">server</span> www.helloworld.com:9000</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token directive"><span class="token keyword">upstream</span> api_server</span><span class="token punctuation">{</span>
  <span class="token directive"><span class="token keyword">server</span> www.helloworld.com:8080</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token directive"><span class="token keyword">server</span></span> <span class="token punctuation">{</span>
  <span class="token directive"><span class="token keyword">listen</span>       <span class="token number">80</span></span><span class="token punctuation">;</span>
  <span class="token directive"><span class="token keyword">server_name</span>  www.helloworld.com</span><span class="token punctuation">;</span>

  <span class="token directive"><span class="token keyword">location</span> ~ ^/api/</span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword">include</span> enable-cors.conf</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">proxy_pass</span> http://api_server</span><span class="token punctuation">;</span>
    <span class="token directive"><span class="token keyword">rewrite</span> <span class="token string">&quot;^/api/(.*)$&quot;</span> /<span class="token variable">$1</span> break</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>

  <span class="token directive"><span class="token keyword">location</span> ~ ^/</span> <span class="token punctuation">{</span>
    <span class="token directive"><span class="token keyword">proxy_pass</span> http://front_server</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>到此，就完成了。</p> <h2 id="资源"><a href="#资源" class="header-anchor">#</a> 资源</h2> <ul><li><a href="http://tool.oschina.net/apidocs/apidoc?api=nginx-zh" target="_blank" rel="noopener noreferrer">Nginx 的中文维基</a></li> <li><a href="http://tengine.taobao.org/book/index.html" target="_blank" rel="noopener noreferrer">Nginx 开发从入门到精通</a></li> <li><a href="https://github.com/trimstray/nginx-admins-handbook" target="_blank" rel="noopener noreferrer">nginx-admins-handbook</a></li> <li><a href="https://nginxconfig.io/" target="_blank" rel="noopener noreferrer">nginxconfig.io</a> - 一款 Nginx 配置生成器</li></ul></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/mochen1942/edit/main/example/docs/README.md" target="_blank" rel="noopener noreferrer"><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon" style="font-size:1.2em;"><path d="M497.9 142.1l-46.1 46.1c-4.7 4.7-12.3 4.7-17 0l-111-111c-4.7-4.7-4.7-12.3 0-17l46.1-46.1c18.7-18.7 49.1-18.7 67.9 0l60.1 60.1c18.8 18.7 18.8 49.1 0 67.9zM284.2 99.8L21.6 362.4.4 483.9c-2.9 16.4 11.4 30.6 27.8 27.8l121.5-21.3 262.6-262.6c4.7-4.7 4.7-12.3 0-17l-111-111c-4.8-4.7-12.4-4.7-17.1 0zM124.1 339.9c-5.5-5.5-5.5-14.3 0-19.8l154-154c5.5-5.5 14.3-5.5 19.8 0s5.5 14.3 0 19.8l-154 154c-5.5 5.5-14.3 5.5-19.8 0zM88 424h48v36.3l-64.5 11.3-31.1-31.1L51.7 376H88v48z"/></svg>
      在 GitHub 上编辑此页
    </a></div> <div class="last-updated"><span class="prefix">上次编辑于:</span> <span class="time">2023/3/15 20:46:14</span></div></footer> <div class="page-nav"><p class="inner"><!----> <span class="next"><a href="/blog-assets/docs/basic/installation.html">
        Installation
      </a>
      →
    </span></p></div> <!----> <!----></main></div> <div class="search-page" data-v-57e42542><span class="search-close"><svg aria-hidden="true" width="34.56" height="34.56" viewBox="-75.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon" style="font-size:2.16em;"><path d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z"/></svg></span> <div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div> <div class="menu-btn-container" data-v-41ef9fec data-v-57e42542><div class="menu-btn-wrapper" data-v-41ef9fec><div class="menu-btn" data-v-41ef9fec><div class="menu-btn-icon" style="display:;" data-v-41ef9fec><span data-v-41ef9fec></span> <span data-v-41ef9fec></span> <span data-v-41ef9fec></span></div> <div class="menu-text" style="display:none;" data-v-41ef9fec>
        0
      </div> <svg class="menu-svg" data-v-41ef9fec><circle cx="50%" cy="50%" r="48%" class="menu-border" style="stroke-dasharray:0% 314.15926%;" data-v-41ef9fec></circle></svg></div> <div class="menu-btn-child-wrapper" data-v-41ef9fec><a role="button" aria-label="Toggle light" title="Toggle light" class="toggle-mode menu-btn-child" data-v-41ef9fec><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-43.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon" style="font-size:1.2em;"><path d="M224 96l16-32 32-16-32-16-16-32-16 32-32 16 32 16 16 32zM80 160l26.66-53.33L160 80l-53.34-26.67L80 0 53.34 53.33 0 80l53.34 26.67L80 160zm352 128l-26.66 53.33L352 368l53.34 26.67L432 448l26.66-53.33L512 368l-53.34-26.67L432 288zm70.62-193.77L417.77 9.38C411.53 3.12 403.34 0 395.15 0c-8.19 0-16.38 3.12-22.63 9.38L9.38 372.52c-12.5 12.5-12.5 32.76 0 45.25l84.85 84.85c6.25 6.25 14.44 9.37 22.62 9.37 8.19 0 16.38-3.12 22.63-9.37l363.14-363.15c12.5-12.48 12.5-32.75 0-45.24zM359.45 203.46l-50.91-50.91 86.6-86.6 50.91 50.91-86.6 86.6z"/></svg></a> <div class="menu-btn-child" data-v-41ef9fec><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-75.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon" style="font-size:1.2em;" data-v-41ef9fec><path d="M207.029 381.476L12.686 187.132c-9.373-9.373-9.373-24.569 0-33.941l22.667-22.667c9.357-9.357 24.522-9.375 33.901-.04L224 284.505l154.745-154.021c9.379-9.335 24.544-9.317 33.901.04l22.667 22.667c9.373 9.373 9.373 24.569 0 33.941L240.971 381.476c-9.373 9.372-24.569 9.372-33.942 0z"/></svg></div> <div class="menu-btn-child" data-v-41ef9fec><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-75.52 -43.52 599.04 599.04" fill="currentColor" class="ov-icon" style="font-size:1.2em;" data-v-41ef9fec><path d="M240.971 130.524l194.343 194.343c9.373 9.373 9.373 24.569 0 33.941l-22.667 22.667c-9.357 9.357-24.522 9.375-33.901.04L224 227.495 69.255 381.516c-9.379 9.335-24.544 9.317-33.901-.04l-22.667-22.667c-9.373-9.373-9.373-24.569 0-33.941L207.03 130.525c9.372-9.373 24.568-9.373 33.941-.001z"/></svg></div> <!----> <div class="menu-btn-child menu-btn-sidebar" data-v-41ef9fec><svg aria-hidden="true" width="19.2" height="19.2" viewBox="-1.6 -1.6 19.2 19.2" fill="currentColor" class="ov-icon" style="font-size:1.2em;" data-v-41ef9fec><path d="M14 2a1 1 0 011 1v10a1 1 0 01-1 1H2a1 1 0 01-1-1V3a1 1 0 011-1h12zM2 1a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V3a2 2 0 00-2-2H2z"/><path d="M3 4a1 1 0 011-1h2a1 1 0 011 1v8a1 1 0 01-1 1H4a1 1 0 01-1-1V4z"/></svg></div></div></div></div> <!----></div><div class="global-ui"></div></div>
    <script src="/blog-assets/assets/js/app.8304d51e.js" defer></script><script src="/blog-assets/assets/js/4.08820d5d.js" defer></script><script src="/blog-assets/assets/js/1.f05623d2.js" defer></script><script src="/blog-assets/assets/js/13.d806e265.js" defer></script>
  </body>
</html>
